package com.wust.demo1.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @author wlei
 *
 */

public class SimpleMailSender {
	public static final String mailServerHost1 = "smtp.sina.com";
	
	public static final String mailServerHost = "smtp.163.com";
	
	public static final String mailServerPort = "25";
	
	public static final boolean validate= true;
	
	public static final String userName1 = "gamedilong@sina.com";
	
	public static final String userName = "gamedilong@163.com";
	
	public static final String password = "000aaa";
	
	public static final String fromAddress = "gamedilong@163.com";
	
	public static final String fromAddress1 = "gamedilong@sina.com";
	
	public static final String subjectOne = "new pass word";
	
	public boolean sendHTMLMail(MailSenderInfo mailInfo){
		MyAuthenticator authenticator = null;
		Properties pro = mailInfo.getPropertise();
		
		if(mailInfo.isValidate()){
			authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());			
		}
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		try{
			Message mailMessage = new MimeMessage(sendMailSession);
			Address from = new InternetAddress(mailInfo.getFromAddress());
			mailMessage.setFrom(from);
			Address to = new InternetAddress(mailInfo.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, to);
			mailMessage.setSubject(mailInfo.getSubject());
			mailMessage.setSentDate(new Date());
			Multipart mainPart = new MimeMultipart();
			BodyPart html = new MimeBodyPart();
			html.setContent(mailInfo.getContent(),"text/html;charset=utf-8");
			mainPart.addBodyPart(html);
			mailMessage.setContent(mainPart);
			Transport.send(mailMessage);
			return true;
			
		}catch(MessagingException ex){
			ex.printStackTrace();
		}
		return false;
	}
	

	/**
	 * 
	 * @param content email content
	 * @param toEmail the email you want to give her new passowrd
	 */
	public static void sendEmail(String content,String toEmail){
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost(SimpleMailSender.mailServerHost);
		mailInfo.setMailServerPort(SimpleMailSender.mailServerPort);
		mailInfo.setValidate(SimpleMailSender.validate);
		mailInfo.setUserName(SimpleMailSender.userName);
		mailInfo.setPassword(SimpleMailSender.password);
		mailInfo.setFromAddress(SimpleMailSender.fromAddress);
		mailInfo.setToAddress(toEmail);
		mailInfo.setSubject(SimpleMailSender.subjectOne);
		mailInfo.setContent("You new password is :<h1>"+content+"</h1>");
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendHTMLMail(mailInfo);
	}
	
}
